package com.kfh.ybracelet.sim;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kfh.ybracelet.R;
import com.kfh.ybracelet.adapter.LogisticsAdapter;
import com.kfh.ybracelet.base.BaseActivity;
import com.kfh.ybracelet.net.HttpUtil;
import com.kfh.ybracelet.utils.Constant;
import com.kfh.ybracelet.utils.LogUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 新开卡物流信息
 * Created by Aaron on 16/10/13.
 */

public class LogisticsActivity extends BaseActivity {

    private final String TAG = this.getClass().getSimpleName();
    private RecyclerView rv;
    private TextView tvsign;
    private TextView tvfrom;
    private TextView tvnum;
    private TextView tvtel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_logistics);

        String tradeNo = getIntent().getStringExtra("tradeNo");

        LogUtil.e(TAG, "订单号:" + tradeNo);

        initView();

        try {
            getLogisticsInfo(tradeNo);
        } catch (JSONException e) {
            e.printStackTrace();
            showToast("未查询到信息");
        }

    }

    private void initView() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        iv_left.setOnClickListener(this);

        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("物流详情");

        this.tvtel = (TextView) findViewById(R.id.tv_tel);
        this.tvnum = (TextView) findViewById(R.id.tv_num);
        this.tvfrom = (TextView) findViewById(R.id.tv_from);
        this.tvsign = (TextView) findViewById(R.id.tv_sign);

        this.rv = (RecyclerView) findViewById(R.id.rv);
        rv.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(linearLayoutManager);
        rv.setItemAnimator(new DefaultItemAnimator());
//        rv.addItemDecoration(new DividerItemDecoration(this,
//                DividerItemDecoration.VERTICAL_LIST));

    }

    /**
     * 获取订单物流信息
     *
     * @param tradeNo 订单号
     */
    private void getLogisticsInfo(String tradeNo) throws JSONException {

        JSONObject request = new JSONObject();
        request.put("token", sp.getString(Constant.TOKEN, ""));
        request.put("tradeNo", tradeNo);
//        request.put("tradeNo", "20160930150203466628247668751368");

        HttpUtil.setPostRequest("请求中...", this, Constant.HOST_IP_TEST + "/" + Constant.CHECKEXPRESS,
                request.toString(), TAG, new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            JSONObject resutl = new JSONObject(str);

                            handleReustlt(resutl);
                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast("未查询到信息");
                        }

                    }
                }, null);

    }

    private void handleReustlt(JSONObject resutl) throws JSONException {

        resutl = resutl.getJSONObject("re");

//        String logicticsTel = "";//快递公司电话
//        String logicticsComp = "";//快递公司
        String logicticsStatus = "";//快递状态

        JSONObject expressInfo = resutl.getJSONObject("expressInfo");

//        switch (resutl.getString("type")) {
//
//            case "EMS":
//            case "ems":
//                logicticsComp = "EMS";
//                logicticsTel = "40080-11183";
//                break;
//            case "QFKD":
//            case "qfkd":
//                logicticsComp = "全峰速运";
//                logicticsTel = "4001-000-001";
//                break;
//            case "SFEXPRESS":
//            case "sfexpress":
//                logicticsComp = "顺丰速运";
//                logicticsTel = "95338";
//                break;
//            case "STO":
//            case "sto":
//                logicticsComp = "申通快递";
//                logicticsTel = "95543";
//                break;
//            case "TTKDEX":
//            case "ttkdex":
//                logicticsComp = "天天快递";
//                logicticsTel = "4001-888-888";
//                break;
//            case "YTO":
//            case "yto":
//                logicticsComp = "圆通快递";
//                logicticsTel = "021-69777888";
//                break;
//            case "YUNDA":
//            case "yunda":
//                logicticsComp = "韵达快递";
//                logicticsTel = "95546";
//                break;
//            case "CHINAPOST":
//            case "chinapost":
//                logicticsComp = "邮政包裹";
//                logicticsTel = "11185";
//                break;
//            case "ZTO":
//            case "zto":
//                logicticsComp = "中通快递";
//                logicticsTel = "95311";
//                break;
//
//            default:
//                logicticsComp = resutl.getString("type");
//                logicticsTel = "";
//                break;
//        }

        switch (resutl.getInt("deliverystatus")) {

            case 1:
                logicticsStatus = "在途中";
                break;

            case 2:
                logicticsStatus = "派件中";
                break;

            case 3:
                logicticsStatus = "已签收";
                break;

            case 4:
                logicticsStatus = "派送失败";
                break;

        }

        tvsign.setText(logicticsStatus);//是否签收
        tvfrom.setText(expressInfo.getString("name"));//快递公司
        tvnum.setText(resutl.getString("number"));//快递单号
        tvtel.setText(expressInfo.getString("tel"));//快递公司电话

        JSONArray jsonArray = resutl.getJSONArray("list");

        if (jsonArray != null && jsonArray.length() > 0) {

            LogisticsAdapter logisticsAdapter = new LogisticsAdapter(this, jsonArray);
            rv.setAdapter(logisticsAdapter);

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mApplication.cancelPendingRequests(TAG);
    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;
        }

    }
}
